#!/bin/bash

# 安装编译依赖
sudo apt update
sudo apt install -y build-essential autoconf libtool bison re2c \
    libxml2-dev libsqlite3-dev libssl-dev libcurl4-openssl-dev \
    libonig-dev libreadline-dev libzip-dev libpng-dev libjpeg-dev \
    libfreetype6-dev libwebp-dev libsodium-dev libxslt1-dev \
    libicu-dev libxpm-dev libtidy-dev libenchant-2-dev libpq-dev \
    libldap2-dev libpspell-dev libedit-dev libmemcached-dev \
    libgmp-dev libc-client-dev libkrb5-dev redis-server

# 下载PHP 8.2源码
PHP_VERSION="8.2.0"  # 可以替换为最新的8.2.x版本
wget https://www.php.net/distributions/php-${PHP_VERSION}.tar.gz
tar -xzvf php-${PHP_VERSION}.tar.gz
cd php-${PHP_VERSION}

# 配置编译选项
./configure \
    --prefix=/usr/local/php8.2 \
    --with-config-file-path=/usr/local/php8.2/etc \
    --with-config-file-scan-dir=/usr/local/php8.2/etc/conf.d \
    --enable-fpm \
    --with-fpm-user=www-data \
    --with-fpm-group=www-data \
    --enable-bcmath \
    --enable-calendar \
    --enable-ctype \
    --with-curl \
    --enable-dom \
    --enable-fileinfo \
    --enable-filter \
    --enable-gd \
    --with-webp \
    --with-jpeg \
    --with-freetype \
    --with-gettext \
    --with-iconv \
    --with-libxml \
    --enable-mbstring \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-openssl \
    --enable-pcntl \
    --enable-pdo \
    --with-pdo-sqlite \
    --enable-phar \
    --enable-posix \
    --with-readline \
    --enable-session \
    --enable-simplexml \
    --with-sodium \
    --with-sqlite3 \
    --enable-tokenizer \
    --enable-xml \
    --enable-xmlreader \
    --enable-xmlwriter \
    --with-zip \
    --with-zlib \
    --with-pear \
    --enable-opcache

# 编译并安装
make -j$(nproc)
sudo make install

# 创建配置文件和目录
sudo mkdir -p /usr/local/php8.2/etc/conf.d
sudo cp php.ini-production /usr/local/php8.2/etc/php.ini
sudo cp sapi/fpm/php-fpm.conf /usr/local/php8.2/etc/
sudo cp /usr/local/php8.2/etc/php-fpm.conf.default /usr/local/php8.2/etc/php-fpm.conf
sudo cp /usr/local/php8.2/etc/php-fpm.d/www.conf.default /usr/local/php8.2/etc/php-fpm.d/www.conf

# 创建符号链接
sudo ln -s /usr/local/php8.2/bin/php /usr/local/bin/php
sudo ln -s /usr/local/php8.2/bin/phpize /usr/local/bin/phpize
sudo ln -s /usr/local/php8.2/bin/pear /usr/local/bin/pear
sudo ln -s /usr/local/php8.2/bin/pecl /usr/local/bin/pecl
sudo ln -s /usr/local/php8.2/sbin/php-fpm /usr/local/sbin/php-fpm

# 添加环境变量
echo 'export PATH="/usr/local/php8.2/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

# 安装Redis扩展
sudo pecl install redis
echo "extension=redis.so" | sudo tee /usr/local/php8.2/etc/conf.d/redis.ini

# 安装Xdebug扩展
pecl install xdebug
echo "zend_extension=xdebug" | sudo tee /usr/local/php8.2/etc/conf.d/xdebug.ini

# 验证安装
php -v
php -m


#sudo vim /usr/local/php8.2/etc/conf.d/xdebug.ini
#
#zend_extension=xdebug
#xdebug.mode=debug
#xdebug.start_with_request=yes
#xdebug.client_port=9003
#xdebug.client_host=127.0.0.1
#xdebug.idekey=VSCODE
#xdebug.log=/tmp/xdebug.log

#sudo vim /usr/local/php8.2/etc/conf.d/opcache.ini
#
#[opcache]
#zend_extension=opcache.so
#opcache.enable=1
#opcache.memory_consumption=128
#opcache.max_accelerated_files=10000
#opcache.revalidate_freq=60